﻿using Company.Core.Enums;
using Company.Core.Helper;
using Company.Core.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Company.Core.Language
{
    [ExposedService(Lifetime.Singleton, typeof(ILanguageManager))]

    public class LanguageManager : ILanguageManager
    {
        private string Uri {  get; set; }
        private ResourceDictionary Resource {  get; set; }
        public LanguageManager()
        {
            Set(LanguageType.CN);
        }
        public string this[string key] 
        {

            get 
            {
                if(this.Resource != null&&this.Resource.Contains(key))
                {
                    return Resource[key].ToString();
                }
                return this[key];

            }
        }

        public LanguageType Current {get;private set;}

        public void Set(LanguageType language)
        {
            Assert.NotNull(language);
            if (this.Uri == null) 
            {
                ResourceDictionary resourceDictionary = Application.Current.Resources.MergedDictionaries[0];
                string path=resourceDictionary.Source.AbsolutePath;
                this.Uri = path.Remove(path.LastIndexOf("/"));



            }
            string target = $"{Uri}/{language}.xaml";
            this.Resource=(ResourceDictionary)Application.LoadComponent(new Uri(target,UriKind.RelativeOrAbsolute));
            Application.Current.Resources.MergedDictionaries.RemoveAt(0);
            Application.Current.Resources.MergedDictionaries.Insert(0, Resource);

            if (Current != language) 
            {
                Current = language;

            }

        }
    }
}
